package com.cloud.mall.sms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.sms.dao.SmsFlashPromotionProductRelationDao;
import com.cloud.mall.sms.dto.SmsFlashPromotionProduct;
import com.cloud.mall.sms.input.SmsFlashPromotionProductRelationInpDTO;
import com.cloud.mall.sms.mapper.SmsFlashPromotionProductRelationMapper;
import com.cloud.mall.sms.model.SmsFlashPromotionProductRelation;
import com.cloud.mall.sms.out.SmsFlashPromotionProductRelationOut;
import com.cloud.mall.sms.service.SmsFlashPromotionProductRelationService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SmsFlashPromotionProductRelationServiceImpl implements SmsFlashPromotionProductRelationService {
    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;
    @Autowired
    private SmsFlashPromotionProductRelationDao relationDao;


    @Override
    public BaseResponse create(@RequestBody List<SmsFlashPromotionProductRelationInpDTO> relationList) {
        for (SmsFlashPromotionProductRelationInpDTO relation : relationList) {
            SmsFlashPromotionProductRelation productRelation=MallBeanUtils.dtoToDo(relation, SmsFlashPromotionProductRelation.class);
            relationMapper.insert(productRelation);
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse update(Long id,@RequestBody SmsFlashPromotionProductRelationInpDTO smsFlashPromotionProductRelationInpDTO) {
        SmsFlashPromotionProductRelation relation=MallBeanUtils.dtoToDo(smsFlashPromotionProductRelationInpDTO, SmsFlashPromotionProductRelation.class);
        relation.setId(id);
        int count=relationMapper.updateByPrimaryKey(relation);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse delete(Long id) {
        return  BaseResponse.setResultSuccess(relationMapper.deleteByPrimaryKey(id));
    }

    @Override
    public BaseResponse<SmsFlashPromotionProductRelationOut> getItem(Long id) {
        SmsFlashPromotionProductRelation relation = relationMapper.selectByPrimaryKey(id);
        return BaseResponse.setResultSuccess(MallBeanUtils.dtoToDo(relation, SmsFlashPromotionProductRelationOut.class));
    }

    @Override
    public BaseResponse<CommonPage<SmsFlashPromotionProduct>> list(Long flashPromotionId, Long flashPromotionSessionId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<SmsFlashPromotionProduct> flashPromotionProductList=relationDao.getList(flashPromotionId,flashPromotionSessionId);
        return BaseResponse.setResultSuccess(CommonPage.restPage(flashPromotionProductList));
    }
}
